import { ref, computed } from 'vue'
import { defineStore, storeToRefs } from 'pinia'
import { invokeAsyncAPi } from '@/utils/xz-fn'
import { useConfigData } from '@/stores/config-data'


// 保存下载管理窗口
export const useDownloadManageWindow = defineStore('downloadManageWindow', () => {
  const downloadManageWindowId = ref<string | number>('')//下载管理窗口id
  const masterWindowId = ref<string | number>('');//主窗口id
  const downloadFinishWindowId = ref<string | number>('');//下载完成的窗口id
  const userOverLoadWindowId = ref<string | number>('');//用户超载窗口id

  const  setDownloadManageWindowId=(id:string|number) =>{
    downloadManageWindowId.value = id;
  }

  const setMasterWindowId=(id: string | number) =>{
    masterWindowId.value = id;
  }

  const setDownloadFinishWindowId=(id: string | number) =>{
    downloadFinishWindowId.value=id
  }

  const setUserOverLoadWindowId=(id: string | number) =>{
    userOverLoadWindowId.value=id
  }

  const configDataStore = useConfigData();
  const { webPath } = storeToRefs(configDataStore);
  

  const openDownloadManageWindow = async (tab?:string|number) => {//打开下载管理窗口
    if (!downloadManageWindowId.value) {//之前没有创建过窗口，创建窗口
      const _windowId: number = await invokeAsyncAPi('fq_CreateWindow', {
        url:import.meta.env.MODE==='dev' ? 'http://127.0.0.1:2022/#downloadManage':(webPath.value+'index.html#downloadManage'),
        payload: JSON.stringify({id:masterWindowId.value,tab:tab||'1'}),
        wnd_name:'下载中心',
        size: { cx: 920, cy: 560 }
      }) as unknown as number;
      setDownloadManageWindowId(_windowId)
    } else {//已经创建了窗口，通知出现在最前面
      invokeAsyncAPi('fq_NotifyWebMessageEx', downloadManageWindowId.value, 'showWindow');
    }
  }

  const openDownloadFinishWindow = async () => {//创建下载完成窗口
    if (!downloadFinishWindowId.value) {//之前没有创建过窗口，创建窗口
      const _windowId: number = await invokeAsyncAPi('fq_CreateWindow', {
        url:import.meta.env.MODE==='dev' ? 'http://127.0.0.1:2022/#downloadFinish':(webPath.value+'index.html#downloadFinish'),
        payload:masterWindowId.value,
        size: { cx: 220, cy: 120 },
        right_bottom: true,
        center: false,
        wnd_name:'下载完成',
        take_focus:false,
      }) as unknown as number;
      setDownloadFinishWindowId(_windowId)
    } else {//已经创建了窗口，通知出现在最前面
      // invokeAsyncAPi('fq_NotifyWebMessageEx', downloadFinishWindowId.value, 'showWindow');
    }
  }

  const openUserOverLoadWindow = async (id:string) => {//创建用户超载窗口
    if (!userOverLoadWindowId.value) {//之前没有创建过窗口，创建窗口
      const _windowId: number = await invokeAsyncAPi('fq_CreateWindow', {
        url:import.meta.env.MODE==='dev' ? 'http://127.0.0.1:2022/#userOverLoad':(webPath.value+'index.html#userOverLoad'),
        payload:JSON.stringify({id:masterWindowId.value,itemId:id}),
        size: { cx: 300, cy: 180 },
        right_bottom: true,
        center: false,
        wnd_name:'用户下载超限',
        take_focus:false,
      }) as unknown as number;
      setUserOverLoadWindowId(_windowId)
    } else {//已经创建了窗口，通知出现在最前面
      // invokeAsyncAPi('fq_NotifyWebMessageEx', downloadFinishWindowId.value, 'showWindow');
    }
  }

  const getMasterWindowStatus = async () => {//判断主窗口状态
    const status = await invokeAsyncAPi('jsMsg_GetWndStatus');
    return status;
  }

  return {
    downloadManageWindowId,
    masterWindowId,
    downloadFinishWindowId,
    setDownloadManageWindowId,
    setMasterWindowId,
    setDownloadFinishWindowId,
    setUserOverLoadWindowId,
    openDownloadManageWindow,
    openDownloadFinishWindow,
    openUserOverLoadWindow,
    getMasterWindowStatus
  }
})
